package com.mf.common.enums;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public abstract class EnumarationTypeBase implements UserType, Serializable {

	protected static int[] SQL_TYPES = { Types.VARCHAR };

	protected String label;

	protected Object value;

	protected String resourceKey;

	public EnumarationTypeBase() {
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public abstract List<?> fetchList();

	private Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public EnumarationTypeBase resolveFromValue(Object value) {
		List<?> list = fetchList();

		if (list == null)
			return null;

		EnumarationTypeBase result = null;
		for (Iterator<?> itr = list.iterator(); itr.hasNext();) {
			EnumarationTypeBase enm = (EnumarationTypeBase) itr.next();
			if (enm.getValue().toString().equals(value.toString())) {
				result = enm;
				break;
			}
		}

		return result;
	}

	public EnumarationTypeBase resolveFromLabel(Object label) {
		List<?> list = fetchList();

		if (list == null)
			return null;

		EnumarationTypeBase result = null;
		for (Iterator<?> itr = list.iterator(); itr.hasNext();) {
			EnumarationTypeBase enm = (EnumarationTypeBase) itr.next();
			if (enm.getLabel().equals(label.toString())) {
				result = enm;
				break;
			}
		}

		return result;
	}

	public String toString() {
		return getLabel();
	}

	public int[] sqlTypes() {
		return SQL_TYPES;
	}

	public Class<?> returnedClass() {
		return getClass();
	}

	public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException,
			SQLException {

		value = resultSet.getString(names[0]);

		return resultSet.wasNull() ? null : resolveFromValue(value);
	}

	public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException,
			SQLException {
		EnumarationTypeBase enumType = (EnumarationTypeBase) value;
		if (value == null) {
			statement.setNull(index, sqlTypes()[0]);
		} else {
			statement.setString(index, enumType.getValue().toString());
		}
	}

	public boolean equals(Object x, Object y) {
		if (x == y)
			return true;
		if (null == x || null == y)
			return false;
		return x.equals(y);
	}

	public boolean equals(Object obj) {
		if (obj instanceof EnumarationTypeBase)
			return this.getValue().equals(((EnumarationTypeBase) obj).getValue());

		return super.equals(obj);
	}

	public Object assemble(Serializable cached, Object owner) throws HibernateException {
		return cached;
	}

	public Serializable disassemble(Object value) throws HibernateException {
		return (Serializable) value;
	}

	public Object replace(Object original, Object target, Object owner) throws HibernateException {
		return original;
	}

	public Object deepCopy(Object value) {
		return value;
	}

	public boolean isMutable() {
		return false;
	}

	public int hashCode(Object x) throws HibernateException {
		return x.hashCode();
	}

	public int hashCode() {
		return (new Integer(this.getValue().toString())).intValue();
	}

	public String getResourceKey() {
		if (resourceKey == null)
			resourceKey = resolveFromValue(this.value).resourceKey;
		return resourceKey;
	}

	public void setResourceKey(String resourceKey) {
		this.resourceKey = resourceKey;
	}
}
